<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220315
 * Time: 11:05 
 * 角色权限管理
 */
namespace app\admin\controller;

use  Mscore\Core\Controller;
use  app\admin\model\Common as CommonModel;
use  app\admin\model\Role as RoleModel;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lib\ArrayMethod;


class RoleController extends Controller{

    private $model;
    private $login;
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new RoleModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
    }

    /**
     * 账号角色标识列表
     *  * @param array $rolelist
     * @param array $where
     */
    public function ident()
    {
        $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = ' dels < 1 ';
            $datalist = $this->model->ident($where,$page,$limit);
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echoJson($data);
        } else {
            $this->display('identlist.html');
        }
    }

    /**
     * 账号角色标识添加
     *  * @param array $identadd
     * @param array $where
     */
    public function identadd()
    {
       $params = post();
       if($params){
           //post请求则保存
           $datainfo = $this->model->identsave($params);
           $this->echoJson($datainfo);
       }else{
           if(get('id')){
                $identinfo = $this->model->identinfo(get('id'));
           }
            //定义变量identinfo输出
            $identinfo['id']=isset($identinfo['id'])?$identinfo['id']:'';
            $identinfo['ident']=isset($identinfo['ident'])?$identinfo['ident']:'';
            $identinfo['title']=isset($identinfo['title'])?$identinfo['title']:'';
            $identinfo['grade']=isset($identinfo['grade'])?$identinfo['grade']:'';
            $identinfo['ranges']=isset($identinfo['ranges'])?$identinfo['ranges']:0;
            $identinfo['status']=isset($identinfo['status'])?$identinfo['status']:0;
           $this->assign('datainfo', $identinfo);
           $this->display('ident_add.html');
       }
    }

    /**
     * 账号角色标识删除
     *  * @param array $identdel
     * @param array $where
     */
    public function identdel()
    {
       $id = post('id',false);
       $datadel = $this->model->identdel($id);
       $this->echoJson($datadel);
    }

    /**
     * 角色列表
     *  * @param array $rolelist
     * @param array $where
     */
    public function rolelist()
    {
        $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = ' dels < 1 ';
            $datalist = $this->model->role($where,$page,$limit);
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echoJson($data);
        } else {
            $this->display('rolelist.html');
        }

    }

    /**
     * 角色添加
     *  * @param array $roleadd
     * @param array $where
     */
    public function roleadd()
   {
       $params = post();
       if($params){
           //post请求则保存
           $datainfo = $this->model->rolesave($params);
           $this->echoJson($datainfo);
       }else{
           $identlist = (new CommonModel())->identlist('dels < 1');

           if(get('id')){
                $roleinfo = $this->model->roleinfo(get('id'));
            }
            //定义变量roleinfo输出
            $roleinfo['id']=isset($roleinfo['id'])?$roleinfo['id']:'';
            $roleinfo['title']=isset($roleinfo['title'])?$roleinfo['title']:'';
            $roleinfo['ranges']=isset($roleinfo['ranges'])?$roleinfo['ranges']:'';
            $roleinfo['grade']=isset($roleinfo['grade'])?$roleinfo['grade']:0;
            $roleinfo['status']=isset($roleinfo['status'])?$roleinfo['status']:0;

           $this->assign('identlist', $identlist);
           $this->assign('datainfo', $roleinfo);
           $this->display('role_add.html');
       }
   }

    /**
     * 角色删除
     *  * @param array $roledel
     * @param array $where
     */
    public function roledel()
    {
       $datadel = $this->model->roledel(post('id',false));
       $this->echoJson($datadel);
    }

    /**
     * 角色权限关联
     * @param array $roleperlist 
     * @param array $where 
     */ 
    public function roleperlist(){
        $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = ' dels < 1  and status>0 and grade!="superadmin"';
            $datalist = $this->model->role($where,$page,$limit);
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echoJson($data);
        } else {
            $this->assign('role_id',get('id'));
            $this->display('roleper.html');
        } 
    }

    /**
     * 角色权限列表
     * @param array $roleper 
     * @param array $where 
     */ 
    public function roleper(){
        $params = post();
        if($params){
            //post请求则保存
            $datainfo = $this->model->rolepersave($params);
            $this->echoJson($datainfo);
        }else{
            $datarplist=[];
            if(get('role_id')){
                $datarplist = $this->model->roleper(get('role_id'),'dels < 1 and status>0');
                $arr = array('checked' =>0);
                //给二维数组的每一个一维数组添加$arr
                array_walk($datarplist, function (&$value, $key, $arr) {
                    $value = array_merge($value, $arr);
                },$arr);
                foreach ($datarplist as $key => $value) {
                    $datarplist[$key]['operate'] = json_decode($value['operate'],true);
                    if( $value['sid']&&$value['pid']>0){
                        if($this->model->subroleper(get('role_id'),' a.pid='.$value['id'])==0){
                            $datarplist[$key]['checked']=1; 
                        }
                    }
                    //查找子对象
                    if($value['checkoperate']){
                        $datarplist[$key]['operate']=array_filter($datarplist[$key]['operate']);
                        //给二维数组的每一个一维数组添加$arr
                        array_walk($datarplist[$key]['operate'], function (&$value, $key, $arr) {
                            $value = array_merge($value, $arr);
                        },$arr);
                        $datarplist[$key]['checkoperate']=json_decode($value['checkoperate'],true);
                        $checkoperate=array_filter($datarplist[$key]['checkoperate']);
                        // 循环选择是否有选择的子对象
                        foreach ($datarplist[$key]['operate'] as $k => $v) {
                            if(isset($checkoperate[$v['key']])&&$checkoperate[$v['key']]==$v['url']){
                                $datarplist[$key]['operate'][$k]['checked']=1;
                            }/*else{
                                $datarplist[$key]['operate'][$k]['checked']=0;
                            }*/
                        }
                    }
                }
                unset($checkoperate);
            }
            $datalist = ArrayMethod::dealListToTree($datarplist,'0','id','pid','children',false);
            $data = array(
                "code" => 200,
                "data" => $datalist,
                "operate" => $datarplist,
            );
            $this->echoJson($data);
        }
             
    }
    
}
